package logic

import (
	"apple/applerpc/applerpc"
	"apple/applerpc/internal/svc"
	"apple/cache"
	"apple/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *applerpc.LoginRequest) (*applerpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	user := mysql.User{
		Account:  in.Account,
		Password: in.Password,
		Tel:      in.Tel,
	}
	get := cache.Rdb.Get(l.ctx, "sendsms"+in.Tel)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("短信错误")
	}
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("账户不存在,请先去注册")
	}
	return &applerpc.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
